﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace BTM_Client_Application.DeviceViewer
{
    class DeviceControlModel
    {
        public string Station { get; set; }

        public UserControl ControlContainer
        {
            get { return _control; }
            set
            {
                _control = value;
                _control.Tag = Station;
                _control.Foreground = new SolidColorBrush(Colors.White);
                SetStyle();
            }
        }

        private UserControl _control;
        private readonly ResourceDictionary _resource = new ResourceDictionary();

        private void SetStyle()
        {
            _resource.Source = new Uri("../../Styles/PanoramaStyle.xaml", UriKind.Relative);
            var v = _resource.Values;
            foreach (var rv in v)
            {
                var sv = rv as Style;
                if (sv != null)
                {
                    if (sv.TargetType == typeof(UserControl))
                    {
                        _control.Style = sv;
                    }
                }
            }
        }
    }

    class DevicesTab
    {
        public string DeviceTitle { get; set; }
        public WrapPanel ItemsPanel { get; set; }
        public ObservableCollection<DeviceControlModel> Devices { get; set; }

        public DevicesTab()
        {
            ItemsPanel = new WrapPanel();
            Devices = new ObservableCollection<DeviceControlModel>();
            Devices.CollectionChanged += DevicesOnCollectionChanged;
        }

        public DevicesTab(string devTitle)
            : this()
        {
            DeviceTitle = devTitle;
        }

        private void DevicesOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs notifyCollectionChangedEventArgs)
        {

            

            if (notifyCollectionChangedEventArgs.Action == NotifyCollectionChangedAction.Add)
            {
                var items = notifyCollectionChangedEventArgs.NewItems;
                foreach (var item in items)
                {
                    ItemsPanel.Children.Add(((DeviceControlModel)item).ControlContainer);
                }
            }
            if (notifyCollectionChangedEventArgs.Action == NotifyCollectionChangedAction.Remove)
            {
                var items = notifyCollectionChangedEventArgs.OldItems;
                foreach (var item in items)
                {
                    ItemsPanel.Children.Remove(((DeviceControlModel)item).ControlContainer);
                }
            }
        }
    }
}
